﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Threading;
using System.Collections;
using ForumServer;
using ForumServer.Forum;
using ForumServer.Logger;

namespace ForumServer
{
    public class ForumServer
    {

        public static void Main(string[] args)
        {
            Guid serverID = Guid.NewGuid();
            Console.Title = "Remote Server";
            Logger.Logger log = new Logger.Logger(serverID, "Server");
            log4net.ILog logger = log.getLogger();
            List<Forum.Forum> _AllForums = new List<Forum.Forum>();
            
            //------------------------Server Configuration------------------------//
            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
            provider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = 20172;
            // create and register the channel
            TcpChannel x_channel = new TcpChannel(props, null, provider);
            ChannelServices.RegisterChannel(x_channel, false);

            //------------------------Publish the objects that the user can create/edit remotely------------------------//
            RemotingConfiguration.RegisterActivatedServiceType(typeof(User));
            RemotingConfiguration.RegisterActivatedServiceType(typeof(Moderator));
            RemotingConfiguration.RegisterActivatedServiceType(typeof(Forum.Thread));
            RemotingConfiguration.RegisterActivatedServiceType(typeof(Post));

            //------------------------Create the forum localy on the server------------------------//
            Forum.Forum _forum = new Forum.Forum("Movies",null);
            Forum.Forum _forum2 = new Forum.Forum("Music",null);
            //------------------------Add some threads for testing------------------------//
            Forum.Thread thread3 = new Forum.Thread("Inception", "Best movie of 2010!", new User("etai", "pass", "mail", "number", "etai", "hazan"));
            Forum.Thread thread4 = new Forum.Thread("The Dark Knight", "Best movie of 2008!", new User("chezi", "pass", "mail", "number", "chezi", "Halpert"));
            _forum.addThread(thread3);
            _forum.addThread(thread4);
            Forum.Thread thread1 = new Forum.Thread("Adele", "Adele's 19 and 21 are great albums!", new User("etai", "pass", "mail", "number", "etai", "hazan"));
            Forum.Thread thread2 = new Forum.Thread("Irish", "Irish music is owseme!", new User("chezi", "pass", "mail", "number", "chezi", "Halpert"));
            _forum2.addThread(thread1);
            _forum2.addThread(thread2);
            //------------------------Publish the already created object so it can be accessed by the user------------------------//
            //--------------------------Testing the Array of Forums---------------------------------------------------
            _AllForums.Add(_forum);
            _AllForums.Add(_forum2);
            Forum.Authenticator _auth = new Authenticator();
            foreach (Forum.Forum forum in _AllForums)
                {
                    _auth.getForums().Add(forum);
                }
            RemotingServices.Marshal(_auth, "Authenticator");
            for (int i = 0; i < _AllForums.Count; i++)
			{
                RemotingServices.Marshal(_AllForums.ElementAt(i), "Forum" + i.ToString());
            }

            //RemotingServices.Marshal(_forum, "Forum0");
            //RemotingServices.Marshal(_forum2, "Forum1");
            
            Console.WriteLine("Welcome To The Forum Server");
            Console.WriteLine("Press enter to quit");
            Console.ReadLine();

            x_channel.StopListening(null); 
        }
    }
}
